<?php

	// +----------------------------------------------------------------------
	// | 用户积分管理
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

	namespace app\event\ucenter;

	use app\pay\event\Pay as PayEvent;
	use app\model\ucenter\User as UserModel;
	use app\common\event\Base;
	use think\Exception;
	use think\facade\Url;

	class UserPoint extends Base
	{


		/**
		 * 创建充值订单
		 * @param array $param
		 * @param UserModel $user
		 * @return mixed
		 * @author  : 微尘 <yicmf@qq.com>
		 * @datetime: 2019/1/28 21:34
		 */
		public function recharge($param, $user)
		{
			// 开启事务
			try {

				dump($param);exit();
				$log = [];
				$log['change_code'] = 'recharge';
				$log['point'] = 1000;
				$log['message'] = '用户充值积分';
				$log['status'] = 0;
				$result = $user->recharges()->save($log);

				$pay_param = [];
				$pay_param['type'] = 'recharge';
				$pay_param['order_sn'] = $result['data']['order_sn'];
				$pay_param['return_url'] = url('forum/Point/index');
				$event = new PayEvent();
				$data = $event->create($pay_param);
				if (0 !== $data['code']) {
					throw new Exception($result['message']);
				}
				$data['pay'] = $data['data'];
				$data['data'] = $result;
				$data['code'] = 0;
			} catch (Exception $e) {
				$data['data'] = $e->getMessage();
				$data['code'] = 1;
			}
			return $data;
		}


		/**
		 * 后台充值
		 * @param array $param
		 * @param UserModel $admin
		 * @return mixed
		 * @author  : 微尘 <yicmf@qq.com>
		 */
		public function changeByAdmin($param, $admin)
		{
			// 开启事务
			try {
				$user = UserModel::find($param['user_id']);
				if (!$user) {
					throw new Exception('用户不存在');
				}
				if ('add' == $param['type']) {
					$param['change_code'] = 'admin_add';
				} else {
					$param['change_code'] = 'admin_reduce';
				}
				$param['relation_id'] = $user['id'];
				$re = self::change($param, $user, $admin);
				$data['message'] = '变更成功';
				$data['code'] = 0;
			} catch (Exception $e) {
				$data['message'] = $e->getMessage();
				$data['code'] = 1;
			}
			return $data;
		}


		/**
		 * 用户行为赠送积分
		 * @param       $user
		 * @param       $code
		 * @param array $param
		 * @throws Exception
		 * @author  : 微尘 <yicmf@qq.com>
		 * @datetime: 2019/4/3 16:14
		 */
		public static function action($user, $code, $param)
		{
			if (isset($param['point']) && $param['point']) {
				$point = [];
				$point['change_code'] = $code;
				$point['relation_id'] = $param['relation_id'];
				$point['point'] = (int)$param['point'];
				self::change($point, $user);
			}
		}

		/**
		 * 积分变更，一般用于其他途径调用
		 * @param array $param
		 * @param UserModel $user
		 * @param UserModel $admin
		 * @return mixed
		 * @throws Exception
		 * @author  : 微尘 <yicmf@qq.com>
		 */
		public static function change($param, &$user, $admin = null)
		{
			$log = [];
			$log['change_code'] = $param['change_code'];
			$user['point'] = $user['point'] + $param['point'];
			$log['point'] = $param['point'];
			$log['relation_id'] = $param['relation_id'];
			isset($admin) && $log['admin_id'] = $admin['id'];
			switch ($param['change_code']) {

				case 'add_question':
					// 兑换礼品
					$log['message'] = '帖子扣除积分';
					break;
				case 'reward_question':
					// 兑换礼品
					$log['message'] = '悬赏帖子扣除积分';
					break;
				case 'user_sign':
					// 兑换礼品
					$log['message'] = '每日签到';
					break;
				case 'user_point_exchange':
					// 兑换礼品
					$log['message'] = isset($param['message'])?:'积分兑换礼品';
					break;
				case 'user_login':
					// 登录赠送
					$log['message'] = '登录赠送';
					break;
				case 'online':
					// 发布问题
					$log['message'] = '每日在线学习30分钟送积分';
					break;
				case 'admin_add':
					// 管理员增加积分
					$log['message'] = '后台管理员增加积分：' . $param['message'];
					$log['remark'] = $param['remark'];
					break;
				case 'admin_reduce':
					// 管理员增加积分
					$log['message'] = '后台管理员减少积分：' . $param['message'];
					$log['remark'] = $param['remark'];
					break;
				case 'gift':
					// 管理员增加积分
					$log['message'] = $param['message'];
					$log['remark'] = $param['remark'];
					break;
				default:
					isset($param['message']) && $log['message'] = $param['message'];
					isset($param['remark']) && $log['remark'] = $param['remark'];
					break;
			}
			if (!$user->save()) {
				throw new Exception('用户信息保存失败');
			}
			// 增加积分变动日志
			if (!$user->points()->save($log)) {
				throw new Exception('用户积分变更信息保存失败');
			}
			return $user;
		}

	}
